﻿namespace Vdc.Grid
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;

    [ParseChildren(true, "Template"), ToolboxItem(false), PersistChildren(false)]
    public class GridTemplate : Control, INamingContainer, ICloneable
    {
        private TemplateContainer _container;
        private string _ControlID;
        private string _ControlPropertyName;
        private ITemplate _template;
        private bool _UseDoubleQuotes = true;
        private bool clearChildControls;
        private bool doubleQuotesSet;
        private bool isDataBound;

        public virtual object Clone()
        {
            GridTemplate template;
            if (this is GridRuntimeTemplate)
            {
                template = new GridRuntimeTemplate();
            }
            else
            {
                template = new GridTemplate();
            }
            template.SetTemplate(this.GetTemplate());
            template.ControlID = this.ControlID;
            template.ControlPropertyName = this.ControlPropertyName;
            template.UseQuotes = this.UseQuotes;
            return template;
        }

        protected override void CreateChildControls()
        {
            if (!base.ChildControlsCreated)
            {
                this._container = new TemplateContainer();
                ITemplate template = this.GetTemplate();
                if (template != null)
                {
                    template.InstantiateIn(this._container);
                }
                if (this.clearChildControls)
                {
                    this.Controls.Clear();
                }
                this.Controls.Add(this._container);
            }
        }

        public override void DataBind()
        {
            this.CreateChildControls();
            base.ChildControlsCreated = true;
            if ((!(this is GridRuntimeTemplate) && (this.NamingContainer != null)) || ((this is GridRuntimeTemplate) && !this.isDataBound))
            {
                base.DataBind();
            }
            this.isDataBound = true;
        }

        internal void EnsureChildControlsCreated()
        {
            if (!this.clearChildControls)
            {
                this.clearChildControls = true;
                this.DataBind();
            }
        }

        internal void ForceDataBind()
        {
            this.CreateChildControls();
            base.ChildControlsCreated = true;
            base.DataBind();
            this.isDataBound = true;
        }

        protected virtual ITemplate GetTemplate()
        {
            return this.Template;
        }

        protected virtual void SetTemplate(ITemplate template)
        {
            this.Template = template;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TemplateContainer Container
        {
            get
            {
                if (this._container == null)
                {
                    this.EnsureChildControls();
                }
                return this._container;
            }
            set
            {
                this._container = value;
            }
        }

        [Bindable(true), Description("The ID of the control to be used"), Category("Template Settings"), DefaultValue("")]
        public string ControlID
        {
            get
            {
                return this._ControlID;
            }
            set
            {
                this._ControlID = value;
            }
        }

        [Bindable(true), Category("Template Settings"), DefaultValue(""), Description("The name of the property of the control used to update the database")]
        public string ControlPropertyName
        {
            get
            {
                return this._ControlPropertyName;
            }
            set
            {
                this._ControlPropertyName = value;
            }
        }

        [TemplateContainer(typeof(TemplateContainer)), Description("The template."), PersistenceMode(PersistenceMode.InnerDefaultProperty), Browsable(false), DefaultValue((string) null)]
        public virtual ITemplate Template
        {
            get
            {
                return this._template;
            }
            set
            {
                this._template = value;
            }
        }

        [DefaultValue(true), Bindable(true), Description("If true, the value will be surrounded by double quotes"), Category("Template Settings")]
        public bool UseQuotes
        {
            get
            {
                if (!this.doubleQuotesSet)
                {
                    if (this.ControlPropertyName == "checked")
                    {
                        this.UseQuotes = false;
                    }
                    else
                    {
                        this.UseQuotes = true;
                    }
                }
                return this._UseDoubleQuotes;
            }
            set
            {
                this._UseDoubleQuotes = value;
                this.doubleQuotesSet = true;
            }
        }
    }
}

